package com.xrtn.controller;

import B.LI;
import B.S;
import C.L;
import com.xrtn.mapper.ImageBrowerMapper;
import com.xrtn.utils.ResponseData;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("imageBrower")
public class ImageBrowerController {

    @Autowired
    ImageBrowerMapper imageBrowerMapper;

    /**
     * 进图片浏览页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoOrder(ModelAndView modelAndView) {
        modelAndView.setViewName("imagesBrower");
        return modelAndView;
    }

    /**
     * 进图片编辑
     *
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView gotoEdit(ModelAndView modelAndView) {
        modelAndView.setViewName("imagesEditing");
        return modelAndView;
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData imgList(Integer lastId, Integer flag) {
        ResponseData responseData = new ResponseData();
        if (null == flag || flag == 0) {
            responseData.setResults(this.imageBrowerMapper.fetchImagesList(lastId));
        } else if (flag == 1) {
            responseData.setResults(this.imageBrowerMapper.fetchDuplicateRemovalImagesList(lastId));
        } else {
            responseData.setResults(this.imageBrowerMapper.fetchSelectImagesList(lastId));
        }
        return responseData;
    }

    /**
     * 症状与结论列表
     *
     * @return
     */
    @RequestMapping(value = "/tongueConclusionList", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData tongueConclusionList() {
        ResponseData responseData = new ResponseData();
        List<Map<String, String>> maps = this.imageBrowerMapper.tongueConclusionList();
        List<Object> resule = new ArrayList<>();

        List<Map<String, String>> value = new ArrayList<>();
        List<Map<String, String>> value2 = new ArrayList<>();
        List<Map<String, String>> value3 = new ArrayList<>();
        List<Map<String, String>> value4 = new ArrayList<>();
        List<Map<String, String>> value5 = new ArrayList<>();
        List<Map<String, String>> value6 = new ArrayList<>();
        List<Map<String, String>> value7 = new ArrayList<>();
        List<Map<String, String>> value8 = new ArrayList<>();
        List<Map<String, String>> value9 = new ArrayList<>();
        List<Map<String, String>> value10 = new ArrayList<>();
        List<Map<String, String>> value11 = new ArrayList<>();
        List<Map<String, String>> value12 = new ArrayList<>();
        List<Map<String, String>> value13 = new ArrayList<>();
        List<Map<String, String>> value14 = new ArrayList<>();
        List<Map<String, String>> value15 = new ArrayList<>();
        List<Map<String, String>> value16 = new ArrayList<>();
        List<Map<String, String>> value17 = new ArrayList<>();
        List<Map<String, String>> value18 = new ArrayList<>();
        List<Map<String, String>> value19 = new ArrayList<>();
        List<Map<String, String>> value20 = new ArrayList<>();
        List<Map<String, String>> value21 = new ArrayList<>();
        List<Map<String, String>> value22 = new ArrayList<>();
        List<Map<String, String>> value23 = new ArrayList<>();

        for (int i = 0; i < maps.size(); i++) {
            String key = maps.get(i).get("type");
            String keyTwo = maps.get(i).get("type_flag");
            if (key.trim().trim().equals("1")) {
                if (keyTwo.trim().equals("1")) {
                    value.add(maps.get(i));
                }
                if (keyTwo.trim().equals("2")) {
                    value2.add(maps.get(i));
                }
                if (keyTwo.trim().equals("3")) {
                    value3.add(maps.get(i));
                }
                if (keyTwo.trim().equals("4")) {
                    value4.add(maps.get(i));
                }
                if (keyTwo.trim().equals("5")) {
                    value5.add(maps.get(i));
                }
                if (keyTwo.trim().equals("6")) {
                    value6.add(maps.get(i));
                }
                if (keyTwo.trim().equals("7")) {
                    value7.add(maps.get(i));
                }
                if (keyTwo.trim().equals("8")) {
                    value8.add(maps.get(i));
                }
                if (keyTwo.trim().equals("9")) {
                    value9.add(maps.get(i));
                }
                if (keyTwo.trim().equals("10")) {
                    value10.add(maps.get(i));
                }
                if (keyTwo.trim().equals("11")) {
                    value11.add(maps.get(i));
                }
            }
            if (key.trim().equals("2")) {
                if (keyTwo.trim().equals("1")) value12.add(maps.get(i));
                if (keyTwo.trim().equals("2")) value13.add(maps.get(i));
                if (keyTwo.trim().equals("3")) value14.add(maps.get(i));
                if (keyTwo.trim().equals("4")) value15.add(maps.get(i));
                if (keyTwo.trim().equals("5")) value16.add(maps.get(i));
                if (keyTwo.trim().equals("6")) value17.add(maps.get(i));
            }
            if (key.trim().equals("3")) {
                if (keyTwo.trim().equals("1")) {
                    value18.add(maps.get(i));
                }
                if (keyTwo.trim().equals("2")) value19.add(maps.get(i));
                if (keyTwo.trim().equals("3")) value20.add(maps.get(i));
                if (keyTwo.trim().equals("4")) value21.add(maps.get(i));
                if (keyTwo.trim().equals("5")) value22.add(maps.get(i));
                if (keyTwo.trim().equals("6")) value23.add(maps.get(i));
            }
        }

//        Map<String, List<Map<String, String>>> map = new LinkedHashMap<>();
//        map.put("TongueBody_One", value);
//        map.put("TongueBody_Two", value2);
//        map.put("TongueBody_Three", value3);
//        map.put("TongueBody_Four", value4);
//        map.put("TongueBody_Five", value5);
//        map.put("TongueBody_Sex", value6);
//        map.put("TongueBody_Seven", value7);
//        map.put("TongueBody_Eight", value8);
//        map.put("TongueBody_Nine", value9);
//        map.put("TongueBody_Ten", value10);
//        map.put("TongueBody_Eleven", value11);
        List<Object> bb = new ArrayList<>();
        bb.add(value);
        bb.add(value2);
        bb.add(value3);
        bb.add(value4);
        bb.add(value5);
        bb.add(value6);
        bb.add(value7);
        bb.add(value8);
        bb.add(value9);
        bb.add(value10);
        bb.add(value11);
        Map<String, Object> mapq = new LinkedHashMap<>();
        mapq.put("TongueBody", bb);
        resule.add(mapq);
//        Map<String, List<Map<String, String>>> map2 = new LinkedHashMap<>();
//        map2.put("CoatingTongue_One", value12);
//        map2.put("CoatingTongue_Two", value13);
//        map2.put("CoatingTongue_Three", value14);
//        map2.put("CoatingTongue_Four", value15);
//        map2.put("CoatingTongue_Five", value16);
//        map2.put("CoatingTongue_Sex", value17);
        List<Object> cc = new ArrayList<>();
        cc.add(value12);
        cc.add(value13);
        cc.add(value14);
        cc.add(value15);
        cc.add(value16);
        cc.add(value17);
        Map<String, Object> mapw = new LinkedHashMap<>();
        mapw.put("CoatingTongue", cc);
        resule.add(mapw);
//        Map<String, List<Map<String, String>>> map3 = new LinkedHashMap<>();
//        map3.put("DiagnosisConclusion_One", value18);
//        map3.put("DiagnosisConclusion_Two", value19);
//        map3.put("DiagnosisConclusion_Three", value20);
//        map3.put("DiagnosisConclusion_Four", value21);
//        map3.put("DiagnosisConclusion_Five", value22);
//        map3.put("DiagnosisConclusion_Sex", value23);
        List<Object> aa = new ArrayList<>();
        aa.add(value18);
        aa.add(value19);
        aa.add(value20);
        aa.add(value21);
        aa.add(value22);
        aa.add(value23);
        Map<String, Object> mape = new LinkedHashMap<>();
        mape.put("DiagnosisConclusion", aa);
        resule.add(mape);
        responseData.setResults(groupList(maps));
        return responseData;
    }

    private List groupList(List<Map<String, String>> list) {
        List<Map> group = new ArrayList();

        for (int i = 0; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            String type = map.get("type").toString();
            String type_title = map.get("type_title").toString();

            Map<String, Object> map1 = null;
            //从group中查找key=type的Map
            for (int j = 0; j < group.size(); j++) {
                if (group.get(j).get("type").equals(type)) {
                    map1 = group.get(j);
                    break;
                }
            }
            if (map1 == null) {
                //没有找到map,初始化一个map
                map1 = new HashMap<>();
                map1.put("type", type);
                map1.put("title", type_title);
                map1.put("list", new ArrayList());
                group.add(map1);
            }
            //找到map后，

            List<Map<String, Object>> list1 = (List) map1.get("list");
            String typeFlag = map.get("type_flag").toString();
            String typeFlagTitle = map.get("type_flag_title").toString();

            Map<String, Object> map2 = null;
            for (int j = 0; j < list1.size(); j++) {
                if (list1.get(j).get("type").equals(typeFlag)) {
                    map2 = list1.get(j);
                    break;
                }
            }

            if (map2 == null) {
                map2 = new HashMap<>();
                map2.put("type", typeFlag);
                map2.put("title", typeFlagTitle);
                map2.put("list", new ArrayList<>());
                list1.add(map2);
            }
            List<Map<String, String>> list2 = (List<Map<String, String>>) map2.get("list");
            list2.add(map);
        }
        return group;
    }


    /**
     * 存储答案
     *
     * @return
     */
    @RequestMapping(value = "/saveSelet", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData saveSelet(String imageurl, String selectContent) {
        ResponseData responseData = new ResponseData();
        List<String> data = this.imageBrowerMapper.select(imageurl);
        if (null == data || data.size() == 0) {
            this.imageBrowerMapper.saveSelect(imageurl, selectContent);
        } else {
            this.imageBrowerMapper.updataSelect(imageurl, selectContent);
        }
        responseData.setResults(0);
        return responseData;
    }

    /**
     * 删除图片
     *
     * @return
     */
    @RequestMapping(value = "/deleteSelet", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData saveSelet(String imageurl) {
        ResponseData responseData = new ResponseData();
        List<String> data = this.imageBrowerMapper.select(imageurl);
        if (null == data || data.size() == 0) {
            this.imageBrowerMapper.saveSelectDelete(imageurl, "0");
        } else {
            this.imageBrowerMapper.updataDeleteSelect(imageurl, "0");
        }
        responseData.setResults(0);
        return responseData;
    }

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData select(String imageurl) {
        ResponseData responseData = new ResponseData();
        List<Map<String, String>> data = this.imageBrowerMapper.selectByImageUrl(imageurl);
        responseData.setResults(data);
        return responseData;
    }
}